package org.example.io.multireactor;

import java.io.IOException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class Acceptor implements Runnable{
    Selector selector;
    ServerSocketChannel serverSocketChannel;

    public Acceptor(Selector selector, ServerSocketChannel serverSocketChannel){
        this.selector = selector;
        this.serverSocketChannel = serverSocketChannel;
    }

    @Override
    public void run() {
        try {
            SocketChannel socket = serverSocketChannel.accept();
            System.out.println("收到一个客户端连接");
            socket.configureBlocking(false);
            socket.register(selector, SelectionKey.OP_READ, new MultiHandler(socket));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
